/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "zobjects_gtk.h"

ZlangInvokeFunction ZlangInvokeFunction_guimenu_Create_guiwindow;
int ZlangInvokeFunction_guimenu_Create_guiwindow( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	struct ZlangDirectProperty_guimenu		*guimenu_direct_prop = GetObjectDirectProperty(out1) ;
	struct ZlangDirectProperty_guiwidget_common	*guimenu_common = & (guimenu_direct_prop->common) ;

	EVAL_GTK_WIDGET_NEW_IN_CREATE( guimenu_common->menu , g_menu_new() )

	TRANSFER_INPUT_OBJECT_TO_NEW_OBJECT( guimenu_common->guiwindow_obj , in1 )

	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guimenu_AppendMenuItem_object;
int ZlangInvokeFunction_guimenu_AppendMenuItem_object( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guimenubar		*guimenu_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangDirectProperty_guiwidget_common	*guimenu_common = & (guimenu_direct_prop->common) ;
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	struct ZlangDirectProperty_guimenuitem		*guimenuitem_direct_prop = GetObjectDirectProperty(in1) ;
	struct ZlangDirectProperty_guiwidget_common	*guimenuitem_common = & (guimenuitem_direct_prop->common) ;

	if( guimenu_direct_prop->common.menu == NULL || guimenuitem_common->menu_item == NULL )
	{
		CallRuntimeFunction_bool_SetBoolValue( rt , out1 , FALSE );
		return 0;
	}

	g_menu_append_item( guimenu_common->menu , guimenuitem_common->menu_item );

	CallRuntimeFunction_bool_SetBoolValue( rt , out1 , TRUE );
	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guimenu_AppendMenuSection_object;
int ZlangInvokeFunction_guimenu_AppendMenuSection_object( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guimenubar		*guimenu_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangDirectProperty_guiwidget_common	*guimenu_common = & (guimenu_direct_prop->common) ;
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	struct ZlangDirectProperty_guimenuitem		*guimenusection_direct_prop = GetObjectDirectProperty(in1) ;
	struct ZlangDirectProperty_guiwidget_common	*guimenusection_common = & (guimenusection_direct_prop->common) ;

	if( guimenu_common->menu == NULL || guimenusection_common->menu == NULL )
	{
		CallRuntimeFunction_bool_SetBoolValue( rt , out1 , FALSE );
		return 0;
	}

	g_menu_append_section( guimenu_common->menu , NULL , G_MENU_MODEL(guimenusection_common->menu) );

	CallRuntimeFunction_bool_SetBoolValue( rt , out1 , TRUE );
	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guimenu_CreateSubMenu_string;
int ZlangInvokeFunction_guimenu_CreateSubMenu_string( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guimenubar		*guimenu_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangDirectProperty_guiwidget_common	*guimenu_common = & (guimenu_direct_prop->common) ;
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	char						*label = NULL ;
	struct ZlangDirectProperty_guimenuitem		*guisubmenu_direct_prop = GetObjectDirectProperty(out1) ;
	struct ZlangDirectProperty_guiwidget_common	*guisubmenu_common = & (guisubmenu_direct_prop->common) ;

	CallRuntimeFunction_string_GetStringValue( rt , in1 , & label , NULL );

	EVAL_GTK_WIDGET_NEW_IN_CREATE( guisubmenu_common->menu , g_menu_new() )

	g_menu_append_submenu( guimenu_common->menu , label , G_MENU_MODEL(guisubmenu_common->menu) );
	
	return 0;
}

ZLANG_INVOKE_FUNCTION_guiwidget_all_common_functions( menu )

ZLANG_INVOKE_FUNCTION_guictl_all_common_functions( menu )

ZlangInvokeFunction ZlangInvokeFunction_guimenu_Popup_int_int;
int ZlangInvokeFunction_guimenu_Popup_int_int( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guimenu		*guimenu_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangDirectProperty_guiwidget_common	*guimenu_common = & (guimenu_direct_prop->common) ;
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*in2 = GetInputParameterInLocalObjectStack(rt,2) ;
	int32_t						x ;
	int32_t						y ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	GtkPopoverMenu					*popover = NULL ;
	GdkRectangle					rect ;

	CallRuntimeFunction_int_GetIntValue( rt , in1 , & x );
	CallRuntimeFunction_int_GetIntValue( rt , in2 , & y );

	popover = GTK_POPOVER_MENU( gtk_popover_menu_new_from_model(G_MENU_MODEL(guimenu_common->menu)) ) ;
	rect.x = x ;
	rect.y = y ;
	rect.width = 1 ;
	rect.height = 1 ;
	gtk_popover_set_pointing_to( GTK_POPOVER(popover), & rect );
	gtk_popover_popup( GTK_POPOVER(popover) );
	
	CallRuntimeFunction_bool_SetBoolValue( rt , out1 , TRUE );
	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guimenu_Popup_object_int_int;
int ZlangInvokeFunction_guimenu_Popup_object_int_int( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guimenu		*guimenu_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangDirectProperty_guiwidget_common	*guimenu_common = & (guimenu_direct_prop->common) ;
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*in2 = GetInputParameterInLocalObjectStack(rt,2) ;
	struct ZlangObject				*in3 = GetInputParameterInLocalObjectStack(rt,3) ;
	int32_t						x ;
	int32_t						y ;
	void						*guictl_direct_prop = GetObjectDirectProperty(in1) ;
	struct ZlangDirectProperty_guiwidget_common	*guictl_common = (struct ZlangDirectProperty_guiwidget_common *)guictl_direct_prop ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	GtkPopoverMenu					*popover = NULL ;
	GdkRectangle					rect ;

	CallRuntimeFunction_int_GetIntValue( rt , in2 , & x );
	CallRuntimeFunction_int_GetIntValue( rt , in3 , & y );

	popover = GTK_POPOVER_MENU( gtk_popover_menu_new_from_model(G_MENU_MODEL(guimenu_common->menu)) ) ;
	gtk_widget_set_parent( GTK_WIDGET(popover) , guictl_common->gtk_widget );
	rect.x = x ;
	rect.y = y ;
	rect.width = 1 ;
	rect.height = 1 ;
	gtk_popover_set_pointing_to( GTK_POPOVER(popover), & rect );
	gtk_popover_popup( GTK_POPOVER(popover) );
	
	CallRuntimeFunction_bool_SetBoolValue( rt , out1 , TRUE );
	return 0;
}

ZlangCreateDirectPropertyFunction ZlangCreateDirectProperty_guimenu;
void *ZlangCreateDirectProperty_guimenu( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guimenu		*guimenu_prop = NULL ;
	
	guimenu_prop = (struct ZlangDirectProperty_guimenu *)ZLMALLOC( sizeof(struct ZlangDirectProperty_guimenu) ) ;
	if( guimenu_prop == NULL )
	{
		SET_RUNTIME_ERROR( rt , RUNTIME_ERROR , ZLANG_ERROR_ALLOC , "alloc memory for entity" )
		return NULL;
	}
	memset( guimenu_prop , 0x00 , sizeof(struct ZlangDirectProperty_guimenu) );

	INIT_ZLANGDIRECTPROPERTY_guiwidget_common(guimenu)
	
	return guimenu_prop;
}

ZlangDestroyDirectPropertyFunction ZlangDestroyDirectProperty_guimenu;
void ZlangDestroyDirectProperty_guimenu( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guimenu	*guimenu_direct_prop = GetObjectDirectProperty(obj) ;
	
	CLEAN_ZLANGDIRECTPROPERTY_guiwidget_common(guimenu)

	ZLFREE( guimenu_direct_prop );
	
	return;
}

ZlangSummarizeDirectPropertySizeFunction ZlangSummarizeDirectPropertySize_guimenu;
void ZlangSummarizeDirectPropertySize_guimenu( struct ZlangRuntime *rt , struct ZlangObject *obj , size_t *summarized_obj_size , size_t *summarized_direct_prop_size )
{
	SUMMARIZE_SIZE( summarized_direct_prop_size , sizeof(struct ZlangDirectProperty_guimenu) )
	return;
}

static struct ZlangDirectFunctions direct_funcs_guimenu =
	{
		ZLANG_OBJECT_guimenu , /* char *ancestor_name */
		
		ZlangCreateDirectProperty_guimenu , /* ZlangCreateDirectPropertyFunction *create_entity_func */
		ZlangDestroyDirectProperty_guimenu , /* ZlangDestroyDirectPropertyFunction *destroy_entity_func */
		
		NULL , /* ZlangFromCharPtrFunction *from_char_ptr_func */
		NULL , /* ZlangToStringFunction *to_string_func */
		NULL , /* ZlangFromDataPtrFunction *from_data_ptr_func */
		NULL , /* ZlangGetDataPtrFunction *get_data_ptr_func */
		
		NULL , /* ZlangOperatorFunction *oper_PLUS_func */
		NULL , /* ZlangOperatorFunction *oper_MINUS_func */
		NULL , /* ZlangOperatorFunction *oper_MUL_func */
		NULL , /* ZlangOperatorFunction *oper_DIV_func */
		NULL , /* ZlangOperatorFunction *oper_MOD_func */
		
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_NEGATIVE_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_NOT_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_BIT_REVERSE_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_PLUS_PLUS_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_MINUS_MINUS_func */
		
		NULL , /* ZlangCompareFunction *comp_EGUAL_func */
		NULL , /* ZlangCompareFunction *comp_NOTEGUAL_func */
		NULL , /* ZlangCompareFunction *comp_LT_func */
		NULL , /* ZlangCompareFunction *comp_LE_func */
		NULL , /* ZlangCompareFunction *comp_GT_func */
		NULL , /* ZlangCompareFunction *comp_GE_func */
		
		NULL , /* ZlangLogicFunction *logic_AND_func */
		NULL , /* ZlangLogicFunction *logic_OR_func */
		
		NULL , /* ZlangLogicFunction *bit_AND_func */
		NULL , /* ZlangLogicFunction *bit_XOR_func */
		NULL , /* ZlangLogicFunction *bit_OR_func */
		NULL , /* ZlangLogicFunction *bit_MOVELEFT_func */
		NULL , /* ZlangLogicFunction *bit_MOVERIGHT_func */
		
		ZlangSummarizeDirectPropertySize_guimenu , /* ZlangSummarizeDirectPropertySizeFunction *summarize_direct_prop_size_func */
	} ;

ZlangImportObjectFunction ZlangImportObject_guimenu;
struct ZlangObject *ZlangImportObject_guimenu( struct ZlangRuntime *rt )
{
	struct ZlangObject	*obj = NULL ;
	struct ZlangFunction	*func = NULL ;
	int			nret = 0 ;
	
	nret = ImportObject( rt , & obj , ZLANG_OBJECT_guimenu , & direct_funcs_guimenu , sizeof(struct ZlangDirectFunctions) , NULL ) ;
	if( nret )
	{
		SET_RUNTIME_ERROR( rt , RUNTIME_ERROR , ZLANG_ERROR_LINK_FUNC_TO_ENTITY , "import object to global objects heap" )
		return NULL;
	}
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guimenu_Create_guiwindow , ZLANG_OBJECT_guimenu , "Create" , ZLANG_OBJECT_guiwindow,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guimenu_AppendMenuItem_object , ZLANG_OBJECT_bool , "AppendMenuItem" , ZLANG_OBJECT_guimenuitem,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guimenu_AppendMenuSection_object , ZLANG_OBJECT_bool , "AppendMenuSection" , ZLANG_OBJECT_guimenu,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guimenu_CreateSubMenu_string , ZLANG_OBJECT_guimenu , "CreateSubMenu" , ZLANG_OBJECT_string,NULL , NULL ) ;
	if( func == NULL )
		return NULL;

	ADD_FUNCTION_AND_PARAMETERS_IN_OBJECT_guiwidget_all_common_functions( menu )

	ADD_FUNCTION_AND_PARAMETERS_IN_OBJECT_guictl_all_common_functions( menu )

	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guimenu_Popup_int_int , ZLANG_OBJECT_bool , "Popup" , ZLANG_OBJECT_int,NULL , ZLANG_OBJECT_int,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guimenu_Popup_object_int_int , ZLANG_OBJECT_bool , "Popup" , ZLANG_OBJECT_object,NULL , ZLANG_OBJECT_int,NULL , ZLANG_OBJECT_int,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	return obj ;
}

